/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DeleteConfirm.java
 *
 * Created on Aug 12, 2010, 4:07:13 PM
 */

package ResultEditor.AnnotationClasses.Mouse;

import java.awt.Dimension;

/**
 *
 * @author Chris
 */
public class DeleteConfirm extends javax.swing.JFrame {

    //protected Manager classmanager;
    protected UserInterface.GUI gui;
    protected String annotation_type_text;

    /** Creates new form DeleteConfirm */
    public DeleteConfirm(UserInterface.GUI gui, String annotation_type_text, boolean is_appeared_in_current_doc) {
        this.gui = gui;
        this.annotation_type_text = annotation_type_text;
        
        initComponents();
        this.setPreferredSize(new Dimension(487,203));
        int x,y;
        int width = this.getWidth(), height = this.getHeight();
        int parent_width = gui.getWidth(), parent_height = gui.getHeight();
        int parent_x = gui.getX(), parent_y = gui.getY();
        x = parent_x + (int)( (parent_width - width)/2 );
        y = parent_y + (int)( (parent_height - height)/2 );
        this.setLocation(x, y);

        if (is_appeared_in_current_doc){
            jRadioButton1.setEnabled(false);
            jRadioButton2.setSelected(true);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Calibri", 0, 14)); // NOI18N
        jLabel1.setText("<html>While you try to delete annotations, do you want to only delete annotations in<br>\ncurrent document? Or in all document?</html>");

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/editcut.png"))); // NOI18N

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setFont(new java.awt.Font("Calibri", 0, 14)); // NOI18N
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("I just want to delete annotations in current document");

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setFont(new java.awt.Font("Calibri", 0, 14)); // NOI18N
        jRadioButton2.setText("I want do delete annotations in all documents.");

        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Delete");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton1)
                            .addComponent(jRadioButton2)))
                    .addComponent(jLabel1))
                .addContainerGap(40, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(343, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jRadioButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jRadioButton2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        ResultEditor.Annotations.Depot depot = new ResultEditor.Annotations.Depot();
        if( jRadioButton1.isSelected() ){
            String filename = ResultEditor.WorkSpace.WorkSet.getCurrentFile().getName().toString().trim();
            deleteAnnotations(filename, annotation_type_text);
            gui.repaintHighlighter();
            gui.showAnnotationCategoriesInTreeView_refresh();
            gui.repaintHighlighter();
            gui.showValidPositionIndicators_setAll();
            gui.showValidPositionIndicators();
            gui.remove_all_underline_highlighter();
            gui.disableAnnotationDisplay();

            this.dispose();
        }
        else
        // mass delete
        {
            deleteAnnotations( null, annotation_type_text );
            gui.repaintHighlighter();
            gui.showAnnotationCategoriesInTreeView_refresh();
            gui.repaintHighlighter();
            gui.showValidPositionIndicators_setAll();
            gui.showValidPositionIndicators();
            gui.remove_all_underline_highlighter();
            gui.disableAnnotationDisplay();
            
            this.dispose();
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void deleteAnnotations(String filename, String annotation_type_text){
        ResultEditor.Annotations.Depot depot = new ResultEditor.Annotations.Depot();
        int amount_of_deleted_annotations = depot.deleteAnnotations_byUniqueText(filename, annotation_type_text);
        if (amount_of_deleted_annotations < 0 )
            gui.showText_OnBottomInfoBar("<html><b><font color=red>Failed</font></b> to delete annotations of \""
                    + annotation_type_text
                    +"\"!!!</html>");
        else if (amount_of_deleted_annotations < 0 )
            gui.showText_OnBottomInfoBar("<html><b><font color=red>Could not<font></b> delete any annotation of \""
                    + annotation_type_text
                    +"\"!</html>");
        else if (filename != null )
            gui.showText_OnBottomInfoBar("<html>Deleted <font color=blue>"
                + amount_of_deleted_annotations
                + "</font> annotations of \"<font color=blue>"
                + annotation_type_text
                + "</font>\" in document \"<font color=blue>"
                + filename + "</font>\".</html>");
        else 
            gui.showText_OnBottomInfoBar("<html>Deleted <font color=blue>"
                + amount_of_deleted_annotations
                + "</font> annotations of \"<font color=blue>"
                + annotation_type_text
                + "</font>\" in all documents.</html>");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    // End of variables declaration//GEN-END:variables

}
